window.APP = window.APP || {
  Routers: {}, 
  Views: {},
  User: {},
  Trans: {},
  BeClient: new BeClient()
};
(function() {
  "use strict";
  window.onerror = function(error, url, line) {
    if (DEBUG){
      console.error("--------------------------");
      console.error('Error name: '+ error + ' in file: '+url+' line: '+line);
      console.error("--------------------------");
    }
    return true; 
  }
  APP.showPreloader = function() {
    $('#preloader').fadeIn(200);
  }
  APP.hidePreloader = function() {
    $('#preloader').stop().fadeOut(200);
  }
  APP.getTrans = function(route, trans) {
    return APP.Trans[lang][route][trans];
  }
  APP.unbindModal = function() {
    $('#modal').unbind();
    $('#modal').on('hide.bs.modal', APP.unbindModal);
  }

  APP.Routers.MainRouter = Backbone.Router.extend({
    routes: {
      "": "main",
      "uzytkownicy": "users",
      "grupy": "groups"
    },
    initialize: function (options) {
      this.language = options.language;
      APP.User = new User();
      $('.rightMenu span[data-lang="'+this.language+'"]').addClass('selected');
      Backbone.history.start({pushState: true, root:'/'+ (this.language != DEFAULT_LANGUAGE ? this.language : '')});

      $('.rightMenu span[data-lang]').on('click', function(e) {
        var $e = $(e.currentTarget);
        if (!$e.hasClass('selected')) {
          $('.rightMenu span[data-lang]').removeClass('selcted');
          $e.addClass('selected');
          var pathname = window.location.pathname;
          var segments = pathname.split('/');
          if (LANGUAGES.indexOf(segments[1]) != -1) {
            pathname = pathname.replace('/'+segments[1], '');
          }
          window.location.href = ($e.attr('data-lang') != DEFAULT_LANGUAGE ? '/' + $e.attr('data-lang') : '') + pathname;
        }
      });
      $('#modal').on('hide.bs.modal', APP.unbindModal);
      new APP.Views.UserInfoView();
      $('#gotoHome').on('click', function(e) {
        e.preventDefault();
        Backbone.history.navigate("", {
          trigger: true
        });
      });
    },
    before: function(route) {
    },
    after: function() {
    },
    main: function() {
      $('#bodyContainer').html(new APP.Views.MainView().renderLoader().el);
    },
    users: function() {
      $('#bodyContainer').html(new APP.Views.UserListView().render().el);
    },
    groups: function() {
      $('#bodyContainer').html(new APP.Views.GroupListView().el);
    }
  });
}());